<template>
  <Description title="内网文件传输" content="内网传输文件的" />

  <div>
    <a-button @click="start" :loading="startLoading">开启服务器</a-button>
    <a-button @click="stop">关闭服务器</a-button>
    <a-button @click="selectFileUrl">选择文件存储路径</a-button>
    <div v-if="fileUrl">选择的文件路径： {{ fileUrl }}</div>
    <div v-if="value">请在浏览器打开：{{ value }}</div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import dayjs, { Dayjs } from "dayjs";
import { useTaskStore } from "@/stores/task";
import Description from '@/components/Description.vue'
import { message } from "ant-design-vue";
const taskStore = useTaskStore();
const value = ref();
const fileUrl = ref();
const startLoading = ref(false)

function start() {
  startLoading.value = true
  window.ipcRenderer
    .invoke("internalNetworkFileTransfer", { type: 'start', url: fileUrl.value })
    .then((res) => {
      if (res.status == 200) {
        value.value = res.data;
      }
    }).finally(() => {
      startLoading.value = false
    })
}
function stop() {
  window.ipcRenderer.invoke("internalNetworkFileTransfer", { type: 'stop' }).then(res => {
    if (res.status == 200) {
      value.value = ''
      message.success('已关闭')
    }
  })
}
function selectFileUrl() {
  window.ipcRenderer.invoke("selectFile").then((res) => {
    if (res.status == 200) {
      fileUrl.value = res.data;
    }
  });
}

const onFinish = (values: any) => {
  console.log("Success:", values);
  const id = Date.now();
  let timer: any = setInterval(() => {
    if (values.time < dayjs().unix() * 1000) {
      if (values.reminderMethod.includes("systemNotice")) {
        window.ipcRenderer.invoke("systemNotice", {
          title: values.title,
          body: values.body,
        });
      }
      if (values.reminderMethod.includes("winTop")) {
        window.ipcRenderer.invoke("setAlwaysOnTop", true);
        setTimeout(() => {
          window.ipcRenderer.invoke("setAlwaysOnTop", false);
        }, 100);
      }
      clearInterval(timer);
      timer = null;
      taskStore.data = taskStore.data.filter((i: any) => i.id != id);
    }
  }, 1000);
  taskStore.data.push({
    id,
    type: "setInterval",
    url: "alarmClock",
    task: timer,
    description: ``
  });
};
</script>

<style></style>
